﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TroublePlayer : Actors 
{
	public int m_PlayerOrder;
	public EmptySquare m_CurrentSquare;

	private PlayerBase m_PlayerInitialBase;
	private Color m_PlayerColor;
	private int m_CurrentPoints = 0;
	private bool m_IsComputer = false;
	private bool m_CPUCanPlay = true;
	private float m_CPUDelay = 0;
	private List<EmptySquare> m_AvailableSquares = new List<EmptySquare>();
	private EmptySquare m_PreviousSquare;

	public int PlayerOrder {get { return m_PlayerOrder; } set { m_PlayerOrder = value; }}
	public EmptySquare CurrentSquare {get { return m_CurrentSquare; } set { m_CurrentSquare = value; }}
	public PlayerBase PlayerInitialBase {get { return m_PlayerInitialBase; } set { m_PlayerInitialBase = value; }}
	public Color PlayerColor {get { return m_PlayerColor; } set { m_PlayerColor = value; }}
	public int CurrentPoints {get { return m_CurrentPoints; } set { m_CurrentPoints = value; }}
	public bool IsComputer {get { return m_IsComputer; } set { m_IsComputer = value; }}
	public bool CPUCanPlay {get { return m_CPUCanPlay; } set { m_CPUCanPlay = value; }}
	
	void Start () 
	{
	
	}
	
	void Update ()
	{
		if(!(GamesManager.Instance.CurrentGame as TroubleGame).GameOver)
		{
			if((GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn == m_PlayerOrder)
			{
				if((GamesManager.Instance.CurrentGame as TroubleGame).CurrentCard)
				{
					if((GamesManager.Instance.CurrentGame as TroubleGame).CurrentCard.CardFlipFinished)
					{
						if(m_IsComputer)
						{
							OnCPUCanMove();
						}
						else
						{
							OnPlayerCanMove();
						}
					}
				}
			}
		}
	}
	
	private void OnCPUCanMove()
	{
		if(m_CPUCanPlay)
		{
			if((GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft != 0)
			{

				if(m_AvailableSquares.Count == 0)
				{
					bool allBlocked = true;
					for(int i = 0; i < m_CurrentSquare.AdjacentSquare.Count; i++)
					{
						if(m_CurrentSquare.AdjacentSquare[i] && ((GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft == 1 || !m_CurrentSquare.AdjacentSquare[i].PlayerOver))
						{
							allBlocked = false;
							if(m_CurrentSquare.AdjacentSquare[i].name != "Base")
							{
								//m_CurrentSquare.AdjacentSquare[i].aRenderer.material.color = (GamesManager.Instance.CurrentGame as TroubleGame).Players[(GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn].PlayerColor;
							}

							m_AvailableSquares.Add (m_CurrentSquare.AdjacentSquare[i]);

							//if(m_CurrentSquare.AdjacentSquare[i].IsHovering)
							//{
							//	m_CurrentSquare.AdjacentSquare[i].aTransform.localScale = new Vector3(m_CurrentSquare.AdjacentSquare[i].InitialScale.x * 1.15f, m_CurrentSquare.AdjacentSquare[i].InitialScale.y * 1.15f);
							//	if(Input.GetMouseButtonDown(0))
							//	{
							//		ChangePosition(m_CurrentSquare.AdjacentSquare[i]);
							//	}
							//}
							//else
							//{
							//	m_CurrentSquare.AdjacentSquare[i].aTransform.localScale = m_CurrentSquare.AdjacentSquare[i].InitialScale;
							//}
						}
					}
					
					if(allBlocked && (GamesManager.Instance.CurrentGame as TroubleGame).CurrentCard.CardValue > 1)
					{
						(GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft = 1;
						(GamesManager.Instance.CurrentGame as TroubleGame).OnPlayerPositionChanged();
					}
				}
				else
				{
					if(m_AvailableSquares.Count > 0)
					{
						int highestValue = 0;
						EmptySquare biggestSquare = null;
						bool foundBiggest = false;
						for(int i = 0; i < m_AvailableSquares.Count; i++)
						{
							if(m_AvailableSquares[i].SquareValue > highestValue)
							{
								foundBiggest = true;
								highestValue = m_AvailableSquares[i].SquareValue;
								biggestSquare = m_AvailableSquares[i];
							}
							//Debug.Log (biggestSquare.SquareValue);
						}

						if(!foundBiggest)
						{
							int randomPosition = Random.Range(0, m_AvailableSquares.Count);
							biggestSquare = m_AvailableSquares[randomPosition];
						}
						ChangePosition(biggestSquare);

						m_AvailableSquares = new List<EmptySquare>();
						m_CPUCanPlay = false;
					}
					else if (m_AvailableSquares.Count == 0)
					{
						ChangePosition(m_AvailableSquares[0]);
						
						m_AvailableSquares = new List<EmptySquare>();
						m_CPUCanPlay = false;
					}
				}
			}
		}
		else
		{
			m_CPUDelay += Time.deltaTime;
			if(m_CPUDelay >= (GamesManager.Instance.CurrentGame as TroubleGame).m_CPUDelayTime)
			{
				m_CPUDelay = 0;
				m_CPUCanPlay = true;
			}
		}
	}

	private void OnPlayerCanMove()
	{
		if((GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft != 0)
		{
			bool allBlocked = true;
			for(int i = 0; i < m_CurrentSquare.AdjacentSquare.Count; i++)
			{
				if(m_CurrentSquare.AdjacentSquare[i] && ((GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft == 1 || !m_CurrentSquare.AdjacentSquare[i].PlayerOver))
				{
					allBlocked = false;
					if(m_CurrentSquare.AdjacentSquare[i].name != "Base")
					{
						m_CurrentSquare.AdjacentSquare[i].aRenderer.material.color = (GamesManager.Instance.CurrentGame as TroubleGame).Players[(GamesManager.Instance.CurrentGame as TroubleGame).CurrentTurn].PlayerColor;
					}
					if(m_CurrentSquare.AdjacentSquare[i].IsHovering)
					{
						m_CurrentSquare.AdjacentSquare[i].aTransform.localScale = new Vector3(m_CurrentSquare.AdjacentSquare[i].InitialScale.x * 1.15f, m_CurrentSquare.AdjacentSquare[i].InitialScale.y * 1.15f);
						if(Input.GetMouseButtonDown(0))
						{
							ChangePosition(m_CurrentSquare.AdjacentSquare[i]);
						}
					}
					else
					{
						m_CurrentSquare.AdjacentSquare[i].aTransform.localScale = m_CurrentSquare.AdjacentSquare[i].InitialScale;
					}
				}
			}

			if(allBlocked && (GamesManager.Instance.CurrentGame as TroubleGame).CurrentCard.CardValue > 1)
			{
				(GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft = 1;
				(GamesManager.Instance.CurrentGame as TroubleGame).OnPlayerPositionChanged();
			}
		}
	}

	private void ChangePosition(EmptySquare nextSquare)
	{
		aTransform.position = nextSquare.aTransform.position;

		if((GamesManager.Instance.CurrentGame as TroubleGame).MovesLeft == (GamesManager.Instance.CurrentGame as TroubleGame).CurrentCard.CardValue)
		{
			m_CurrentSquare.SquareValue = 0;
		}

		m_CurrentSquare.SomethingOver = false;
		m_CurrentSquare.PlayerOver = false;
		nextSquare.SomethingOver = true;
		m_CurrentSquare = nextSquare;

		if(m_CurrentSquare.name != "Base")
		{
			m_CurrentSquare.aRenderer.material.color = Color.white;
		}

		(GamesManager.Instance.CurrentGame as TroubleGame).OnPlayerPositionChanged();
	}

	public void ResetTileRenderer()
	{
		for(int i = 0; i < m_CurrentSquare.AdjacentSquare.Count; i++)
		{
			if(m_CurrentSquare.AdjacentSquare[i])
			{
				if(m_CurrentSquare.AdjacentSquare[i].name != "Base")
				{
					m_CurrentSquare.AdjacentSquare[i].aRenderer.material.color = Color.white;
				}

				for(int j = 0; j < m_CurrentSquare.AdjacentSquare[i].AdjacentSquare.Count; j++)
				{
					if(m_CurrentSquare.AdjacentSquare[i].AdjacentSquare[j])
					{
						if(m_CurrentSquare.AdjacentSquare[i].AdjacentSquare[j].name != "Base")
						{
							m_CurrentSquare.AdjacentSquare[i].AdjacentSquare[j].aRenderer.material.color = Color.white;
						}
					}
				}
			}
		}
	}
}
